#!/bin/bash
monite_step=`expr 1 \* 1` # log step 30
process_name="calibration_visible"

restart_count=1
calibration_visible_live=`ps -ef |grep -w $process_name|grep -v grep|wc -l`
if [ $calibration_visible_live -le 0 ];then
    echo "Error: No calibration_visible process"
    sleep 5
    cd /home/pi/calibration_visible/bin
    ./calibration_visible
    while : # monitoring
    do
        calibration_visible_live=`ps -ef |grep -w $process_name|grep -v grep|wc -l`
        if [ $calibration_visible_live -le 0 ];then
            echo "Error: calibration_visible process was killed"
            echo "restart_count: $restart_count" >> /home/pi/restart_num.txt
            date >> /home/pi/restart_num.txt
            cd /home/pi/calibration_visible/bin
            ./calibration_visible
            let restart_count++
        fi
        sleep $monite_step
    done
else
    echo "INFO: calibration_visible is running"
fi
